﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ParseData
{
    public class LightSensorReading : ISensorReading
    {
        private LightSensor sensor;

        private string _Date = "";
        /// <summary>Get or set the reading date.</summary>
        public string Date
        {
            get { return this._Date; }
            set { this._Date = value; }
        }

        private string _Hour = "";
        /// <summary>Get or set the reading hour.</summary>
        public string Hour
        {
            get { return this._Hour; }
            set { this._Hour = value; }
        }

        private double _Value;
        /// <summary>Get or set the reading value.</summary>
        public double Value
        {
            get { return this._Value; }
            set { this._Value = value; }
        }

        /// <summary>Constructor.</summary>
        public LightSensorReading(LightSensor sensor)
        {
            this.sensor = sensor;
        }

        /// <summary>Constructor + value.</summary>
        public LightSensorReading(LightSensor sensor, double value)
        {
            this.sensor = sensor;
            this.Value = value;
        }

        public bool HasValue()
        {
            bool hasVal = false;
            if (_Value > 0) hasVal = true;
            return hasVal;
        }

        /// <summary>Return values format.</summary>
        public string ReadableString
        {
            get
            {
                //String result = String.Format("{0}", _Value);
                String result = _Value.ToString("00.0");
                if (_Date.Length != 0)
                    result += String.Format(" {0}", _Date);
                if (_Hour.Length != 0)
                    result += String.Format(" {0}", _Hour);
                return result;
            }
        }
    }

    /// <summary>
    /// Class to read data files with lighting values of the desired type.
    /// </summary>
    public class LightSensor : ParseData.ISensor
    {
        private string[] fileLines;
        private string _SensorID;

        private double tempSum = 0;
        private int tempCount = 0;

        public ISensorReading AverageValue
        {
            get
            {
                if (tempCount > 0)
                    return new LightSensorReading(this, tempSum / tempCount);
                else
                    return null;
            }
        }

        private double minLight = 0;
        public ISensorReading MinValue
        {
            get
            {
                if (tempCount > 0)
                    return new LightSensorReading(this, minLight);
                else
                    return null;
            }
        }

        private double maxLight = 0;
        public ISensorReading MaxValue
        {
            get
            {
                if (tempCount > 0)
                    return new LightSensorReading(this, maxLight);
                else
                    return null;
            }
        }

        public void ResetMinValue()
        {
            minLight = 0;
        }
        public void ResetMaxValue()
        {
            maxLight = 0;
        }

        LightSensorReading lastReading;

        public string SenzorID
        {
            get { return this._SensorID; }
            set { this._SensorID = value; }
        }

        public string TypeName
        {
            get { return "light sensor"; }
        }

         /// <param name="FileName">Location of the file.</param>
        public LightSensor(string Filename)
        {
            string fileLine = "";
            fileLines = FileReaderUtil.SplitFileByLine(Filename);

            // Sensor's ID
            int indexID = 1;
            {
                fileLine = fileLines[indexID].Trim();
                _SensorID = fileLine.Substring(0, 6).Trim();
            }
        }

        private int fileIndex = 3;

        /// <summary>
        /// Parse the given positional file.
        /// </summary>
        public ISensorReading ReadNewValue()
        {
            if (fileIndex < fileLines.Length)
            {
                string fileLine = fileLines[fileIndex].Trim();
                lastReading = new LightSensorReading(this);

                if (fileLine.Length > 0)
                {
                    lastReading.Value = Double.Parse(fileLine.Substring(0, 5).Trim(), new System.Globalization.CultureInfo("en-US"));
                    lastReading.Date = fileLine.Substring(5, 11).Trim();
                    lastReading.Hour = fileLine.Substring(16, 8).Trim();
            }

            double curentSenzorVal = lastReading.Value;

            //Room lightimg calculations
            //Min lighting in the room       
            if (curentSenzorVal < 8) minLight = curentSenzorVal;
            //Min lighting in the room  
            if (curentSenzorVal > 30) maxLight = curentSenzorVal;
            //Average light
            tempSum = tempSum + curentSenzorVal;
            tempCount++;
            double tempMed = tempSum / tempCount;

            fileIndex++;
            }
            else
            {
                lastReading = null;
            }

            return lastReading;
        }
    }
}
